<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Controller\AuthController;
use Think\Auth;
use Admin\Model\ColumnModel;

class NewsController extends AuthController {

    //文章列表
    public function news_list() {

        $count = M('news')->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 18); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $news = M('news')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $column=D('Column');
        foreach($news as $k=>$v){
            $result=$column->get_column_name($v['news_columnid']);
            $news[$k]['column_name']=$result['column_name'];
        }
        $this->assign('news', $news);
        $this->assign('page', $show);
        $this->display();
    }

    //添加文章
    public function news_listadd() {
        $column = M('column');      //栏目表
        $diyflag = M('diyflag');    //文章类型
        $nav = new \Org\Util\Leftnav;
        $column_next = $column->where('column_type <> 5 and column_type <> 2')->order('column_order')->select();
        $diyflag = $diyflag->select();
        $arr = $nav::column($column_next);
        $this->assign('column', $arr);
        $this->assign('diyflag', $diyflag);
        $this->display();
    }
    //修改文章
    public function news_listedit() {
        $id = I('n_id');
        $news = M('news');
        $news = $news->where(array('n_id' => $id))->find();

        $column = M('column');      //栏目表
        $diyflag = M('diyflag');    //文章类型
        $nav = new \Org\Util\Leftnav;
        $column_next = $column->where('column_type <> 5 and column_type <> 2')->order('column_order')->select();
        $diyflag = $diyflag->select();
        $arr = $nav::column($column_next);
        $this->assign('column', $arr);
        $this->assign('diyflag', $diyflag);
        $this->assign('news', $news);
        $this->display();
    }

    //栏目管理
    public function news_column() {
        $column = M('column');
        $nav = new \Org\Util\Leftnav;   //树状结构
        $column = $column->order('column_order')->select();
        $arr = $nav::column($column);
        $this->assign('arr', $arr);
        $this->display();
    }

    //添加栏目
    public function news_columnadd() {
        $column_leftid = I('c_id', 0);
        $this->assign('column_leftid', $column_leftid);
        $this->display();
    }

    //修改栏目
    public function news_columnedit() {
        $id = I('c_id');
        $column = M('column');
        $column = $column->where(array('c_id' => $id))->find();
        $this->assign('column', $column);
        $this->display();
    }

    //文章增加入库
    public function runnews_add() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_listadd'), 0);
        } else {
            $data = array(
                'news_title' => I('news_name'),
                'news_entitle' => I('news_enname'),
                'news_columnid' => I('news_columnid'),
                'news_open' => I('news_open'),
                'news_content' => I('kind_text'),
                'news_encontent' => I('kind_text1'),
                'news_time' => time(),
            );
            M('news')->add($data);
            $this->success('文章保存成功', U('News/news_list'), 1);
        }
    }

    //文章修改入库
    public function runnews_edit() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_listedit'), 0);
        } else {
            $data = array(
                'news_title' => I('news_name'),
                'news_entitle' => I('news_enname'),
                'news_columnid' => I('news_columnid'),
                'news_open' => I('news_open'),
                'news_content' => I('kind_text'),
                'news_encontent' => I('kind_text1'),
            );

            $result = M('news')->where('n_id='.I('news_id'))->save($data);

            if ($result) {
                $this->success('保存成功', U('News/news_list'), 1);
            } else {
                $this->error('保存失败', U('News/news_list'), 0);
            }
        }
    }

    //栏目增加入库
    public function runnews_columnadd() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_column'), 0);
        } else {
            $data = array(
                'column_name' => I('column_name'),
                'column_enname' => I('column_enname'),
                'column_type' => I('column_type'),
                'column_leftid' => I('column_leftid'),
                'column_address' => I('column_address'),
                'column_open' => I('column_open', 0),
                'column_order' => I('column_order'),
                'column_title' => I('column_title'),
                'column_key' => I('column_key'),
                'column_des' => I('column_des'),
                'column_content' => I('kind_text'),
                'column_encontent' => I('kind_text1'),
            );
            M('column')->add($data);
            $this->success('栏目保存成功', U('News/news_column'), 1);
        }
    }

    //栏目修改入库
    public function runnews_columnedit() {
        if (!IS_AJAX) {
            $this->error('提交方式不正确', U('News/news_column'), 0);
        } else {
            $data = array(
                'column_name' => I('column_name'),
                'column_enname' => I('column_enname'),
                'column_type' => I('column_type'),
                'column_address' => I('column_address'),
                'column_open' => I('column_open', 0),
                'column_order' => I('column_order'),
                'column_title' => I('column_title'),
                'column_key' => I('column_key'),
                'column_des' => I('column_des'),
                'column_content' => I('kind_text'),
                'column_encontent' => I('kind_text1'),
            );
            $result = M('column')->where('c_id=' . I('column_id'))->save($data);
            if ($result) {
                $this->success('栏目保存成功', U('News/news_column'), 1);
            } else {
                $this->error('保存失败', U('News/news_column'), 0);
            }
        }
    }

    //删除栏目
    public function news_columndel() {
        $column = M('column')->where(array('c_id' => I('c_id')))->delete();
        $this->redirect('news_column');
    }

    //多选 删除栏目
    public function leftnavalldel() {
        if (I('left_id')) {
            $ids = implode(',', I('left_id'));
        }
        M('column')->delete($ids);
        $this->success('栏目删除成功', U('News/news_column'), 1);
    }

    //多选 排序栏目
    public function leftnavorder() {
        if (I('left_id')) {
            $column = M('column');
            foreach (I('left_id') as $key => $val) {
                $result = $column->where('c_id=' . $val)->setField('column_order', I($val));
            }
        }
        //var_dump(U('News/news_column'));exit;
        $this->success('排序成功', U('News/news_column'), 1);
    }

}
